package classLoader;

import java.io.InputStream;

public class ClassLoaderTest {
    public static void main(String[] args)
            throws InstantiationException, IllegalAccessException, ClassNotFoundException {
        ClassLoader myLoader = new ClassLoader() {
            public Class<?> loadClass(String name) throws ClassNotFoundException {
                try {
                    String fileName = name.substring(name.lastIndexOf(".") + 1) + ".class";
                    InputStream is = getClass().getResourceAsStream(fileName);
                    if (is == null) {
                        return super.loadClass(name);
                    }
                    byte[] b = new byte[is.available()];
                    is.read(b);
                    return defineClass(name, b, 0, b.length);
                } catch (Exception e) {
                    throw new ClassNotFoundException(name);
                }
            }
        };
        Object obj = myLoader.loadClass("classLoader.classLoaderTest").newInstance();
        System.out.println(obj.getClass());
        System.out.println(obj instanceof classLoader.ClassLoaderTest);
    }
}
/* 
该程序并没有运行起来，提示说java.lang.NoClassDefFoundError:

我也不知道如何解决这个错误

*/